/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ws.y10k.goloim.model.Boxes;
import ws.y10k.goloim.model.Orders;
import ws.y10k.goloim.model.Pallets;
import ws.y10k.goloim.model.Products;

/**
 * @author natx
 *
 */
public final class GoLoIm extends ApplicationWindow {
	
	static Boxes boxes;
	static Pallets pallets;
	static Products products;
	static Orders orders;
	static TreeViewer tvrOrders;
	
	protected GoLoIm() {
		super(null);
		boxes = new Boxes();
		pallets = new Pallets();
		products = new Products();
		orders = new Orders();
	}
	
	protected MenuManager createMenuManager() {
		// TODO i18n
		MenuManager mnuBar = new MenuManager();
		
		MenuManager mnuLoads = new MenuManager("&Loads");
		MenuManager mnuManage = new MenuManager("&Manage");
		
		MenuManager mnuOrders = new MenuManager("&Orders");
		Action addOrder = new AddOrderAction(this);
		mnuOrders.add(addOrder);
		MenuManager mnuProducts = new MenuManager("&Products");
		Action addProduct = new AddProductAction(this);
		mnuProducts.add(addProduct);
		MenuManager mnuHelp = new MenuManager("&Help");
		Action showHelp = new ShowHelpAction(this);
		mnuHelp.add(showHelp);
		Action showAbout = new ShowAboutAction(this);
		mnuHelp.add(showAbout);
		Action quit = new QuitAction(this);
		
		Action managePallets = new ManagePalletsAction(this);
		mnuManage.add(managePallets);
		Action manageBoxes = new ManageBoxesAction(this);
		mnuManage.add(manageBoxes);
		
		mnuLoads.add(mnuOrders);
		mnuLoads.add(mnuProducts);
		mnuLoads.add(new Separator());
		mnuLoads.add(mnuHelp);
		mnuLoads.add(new Separator());
		mnuLoads.add(quit);
		
		mnuBar.add(mnuLoads);
		mnuBar.add(mnuManage);
		
		return mnuBar;
	}
	
	public boolean close() {
		// TODO i18n
		MessageDialog closeConfirmQuestion = new MessageDialog(this.getShell(), "Close confirmation", null, "Do you really want to close the app?", MessageDialog.QUESTION, new String[]{"OK","Cancel"},0);
		closeConfirmQuestion.open();
		if ( closeConfirmQuestion.getReturnCode() == 0 ) {
			super.close();
			return true;
		} else {
			return false;
		}
	}
		
	protected Control createContents(Composite parent) {
		// TODO Make title a const and/or i18n
		GoLoIm.setOptimalSize(parent.getShell(),80);
		GoLoIm.center(parent.getShell());
		parent.getShell().setText("GoLoIm");
		
		Composite cmpMain = new Composite(parent.getShell(),SWT.NONE);
		GridLayout lytMain = new GridLayout();
		lytMain.numColumns = 3;
		cmpMain.setLayout(lytMain);
		cmpMain.setLocation(0,0);
		cmpMain.setSize(parent.getShell().getBounds().width, parent.getShell().getBounds().height);
		
		GridData gdt;
		Composite cmpLeft = new Composite(cmpMain, SWT.NONE);
		cmpLeft.setLocation(0,0);
		cmpLeft.setSize((cmpMain.getBounds().width / 3) - 10, cmpMain.getBounds().height - 50);
		gdt = new GridData();
		gdt.widthHint = (cmpMain.getBounds().width / 3) - 10;
		gdt.heightHint = cmpMain.getBounds().height - 50;
		cmpLeft.setLayoutData(gdt);	
		GridLayout grd = new GridLayout();
		grd.numColumns = 2;
		cmpLeft.setLayout(grd);
		
		tvrOrders = new TreeViewer(cmpLeft,SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		tvrOrders.getTree().setLinesVisible(true);
		tvrOrders.getTree().setHeaderVisible(true);
		TreeViewerColumn column = new TreeViewerColumn(tvrOrders,SWT.NONE);
		column.getColumn().setText("Code");
		column.getColumn().setWidth(cmpLeft.getBounds().width / 2 - 100);
		column.setLabelProvider(new CodeColumnLabelProvider());
		column = new TreeViewerColumn(tvrOrders,SWT.NONE);
		column.getColumn().setText("Customer");
		column.setLabelProvider(new CustomerColumnLabelProvider());
		column.getColumn().setWidth(cmpLeft.getBounds().width / 2 - 100);
		tvrOrders.setContentProvider(new OrdersContentProvider());
		tvrOrders.setInput(orders);
		GridData grdData = new GridData();
		grdData.horizontalAlignment = SWT.BEGINNING;
		tvrOrders.getTree().setSize(cmpLeft.getBounds().width,parent.getShell().getBounds().height - 575);
		tvrOrders.getTree().setLayoutData(grdData);
		tvrOrders.getTree().addListener(SWT.Selection, new OrdersTreeSelectionListener(this));
		
		Composite cmpMiddle = new Composite(cmpMain, SWT.NONE);
		cmpMiddle.setLocation(cmpMain.getBounds().width / 3, 0);
		cmpMiddle.setSize((cmpMain.getBounds().width / 3) - 10, cmpMain.getBounds().height - 50);
		gdt = new GridData();
		gdt.widthHint = (cmpMain.getBounds().width / 3) - 10;
		gdt.heightHint = cmpMain.getBounds().height - 50;
		cmpMiddle.setLayoutData(gdt);
		
		Composite cmpRight = new Composite(cmpMain, SWT.NULL);
		cmpRight.setLocation(cmpMain.getBounds().width / 3 * 2, 0);
		cmpRight.setSize((cmpMain.getBounds().width / 3) - 10, cmpMain.getBounds().height - 50);
		gdt = new GridData();
		gdt.widthHint = (cmpMain.getBounds().width / 3) - 10;
		gdt.heightHint = cmpMain.getBounds().height - 50;
		cmpRight.setLayoutData(gdt);
		
		return parent;
	}

	static void center(Shell shell) {
		shell.setLocation((shell.getDisplay().getPrimaryMonitor().getBounds().width - shell.getBounds().width) / 2, (shell.getDisplay().getPrimaryMonitor().getBounds().height - shell.getBounds().height) / 2);
	}

	static void setOptimalSize(Shell shell, int percent) {
		shell.setSize(shell.getDisplay().getBounds().width * percent / 100, shell.getDisplay().getBounds().height * percent / 100);
	}
	
	public static void main(String[] args) {
		
		GoLoIm goloim = new GoLoIm();

		Image imgLoad = new Image(new Display(), "images/goloim.png");
		// TODO This splash window must include !SWT.CLOSE in its style.
		Shell shlInit = new Shell(Display.getCurrent(),SWT.NO_TRIM | SWT.APPLICATION_MODAL);
		shlInit.setBackgroundImage(imgLoad);
		shlInit.setSize(imgLoad.getBounds().width,imgLoad.getBounds().height);
		GoLoIm.center(shlInit);
		shlInit.open();
		
		GoLoIm.boxes.load();
		GoLoIm.pallets.load();
		
		goloim.setBlockOnOpen(true);
		goloim.addMenuBar();
		goloim.open();
		Display.getCurrent().dispose();
	}

}
